package com.sdp.th.task;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sdp.right.cron.ScheduledOfTask;
import com.sdp.right.db.sys_config.service.ISysConfigService;
import com.sdp.right.entity.SysConfig;
import com.sdp.th.db.th_eq_info.service.IThEqInfoService;
import com.sdp.th.db.th_eq_offline.service.IThEqOfflineService;
import com.sdp.th.db.th_eq_state.service.IThEqStateService;
import com.sdp.th.entity.ThEqInfo;
import com.sdp.th.entity.ThEqOffline;
import com.sdp.th.entity.ThEqState;
import com.sdp.th.utils.ThConstants;
import com.sdp.th.utils.Utils;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class OfflineEqTask implements ScheduledOfTask{
	
	@Autowired
	private ISysConfigService iSysConfigService;
	
	@Autowired
	private IThEqStateService iThEqStateService;
	
	@Autowired
	private IThEqInfoService iThEqInfoService;
	
	@Autowired
	private IThEqOfflineService iThEqOfflineService;

	

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void execute() {
		
		try {
			int thHour = 24;
			int smokeHour = 24;
			
			SysConfig thConfig = iSysConfigService.querySysConfigByParamCode("4438054131416322048");
			
			if(thConfig!=null && !StringUtils.isEmpty(thConfig.getParamVal())) {
				thHour = Integer.parseInt(thConfig.getParamVal());
			}
			
			SysConfig smokeConfig = iSysConfigService.querySysConfigByParamCode("4438054353995452416");
			if(smokeConfig!=null && !StringUtils.isEmpty(smokeConfig.getParamVal())) {
				smokeHour = Integer.parseInt(smokeConfig.getParamVal());
			}
			
			List<String> deviceNos = new ArrayList<>();
			List<ThEqState> eqStateList = iThEqStateService.list();
			int hour = 24;
			LocalDateTime now = LocalDateTime.now();
			for(ThEqState state : eqStateList) {
				if(ThConstants.设备类型_温湿度变送器.getCode().equals(state.getDeviceType())) {
					hour = thHour;
				}else if(ThConstants.设备类型_烟感探测器.getCode().equals(state.getDeviceType())) {
					hour = smokeHour;
				}else {
					continue;
				}
				if(state.getReportTime()!=null) {
					if(Duration.between(state.getReportTime(), now).toHours()>hour) {
						deviceNos.add(state.getDeviceNo());
						state.setLineState(ThConstants.否.getCode());
					}
				}else {
					deviceNos.add(state.getDeviceNo());
					state.setLineState(ThConstants.否.getCode());
				}
			}
			
			if(deviceNos.size()==0) {
				return;
			}
			iThEqOfflineService.removeAll();
			List<ThEqOffline> offlineList = new ArrayList<>();
			List<ThEqInfo> eqInfoList = iThEqInfoService.queryEqInfoByNos(deviceNos);
			for(ThEqInfo eq : eqInfoList) {
				ThEqOffline offline = new ThEqOffline();
				offline.setDeviceName(eq.getDeviceName());
				offline.setDeviceNo(eq.getDeviceNo());
				offline.setDeviceRegion(eq.getDeviceRegion());
				offline.setDeviceRoom(eq.getDeviceRoom());
				offline.setDeviceType(eq.getDeviceType());
				offline.setOrgNo(eq.getOrgNo());
				offline.setCreateTime(LocalDateTime.now());
				offlineList.add(offline);
			}
			
			iThEqOfflineService.saveBatch(offlineList);
			iThEqStateService.updateBatchById(eqStateList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("判断设备是否异常的定时任务执行错误",Utils.stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace()));
		}
		
	}

}
